package com.wangjc.mongo.web;

import com.wangjc.mongo.entity.MongoTextInfo;
import com.wangjc.mongo.service.MongoTextService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wangjc
 * @title: TestController
 * @projectName wangjc-mongo
 * @description: TODO
 */
@RestController
public class TestController {

    @Autowired
    private MongoTextService mongoTextService;

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/insert")
    public String insert(){
        String info = "这是一段长达250KB的富文本信息,强烈不建议把它丢放到MySQL/Oracle中，你会发现届时的读写将变得异常慢";
        MongoTextInfo textInfo = new MongoTextInfo(){{
            setContent(info);
        }};
        String mongoId = mongoTextService.insertOne(textInfo);
        return "存储成功，mongoId:"+mongoId;
    }

    /**
     * 修改
     * @param mongoId
     * @return
     */
    @RequestMapping(value = "/update",method = {RequestMethod.GET,RequestMethod.POST})
    public String update(String mongoId){
        MongoTextInfo textInfo = new MongoTextInfo(){{
            setContent("我也可以用来存储任何长文本信息，减轻关系型数据库的读写压力");
            setMongoId(new ObjectId(mongoId));
        }};
        Long update = mongoTextService.updateOne(textInfo);
        return "修改成功，更新条目:"+update;
    }

    /**
     * 删除
     * @param mongoId
     * @return
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    public String delete(String mongoId){
        Long delete = mongoTextService.deleteOne(mongoId);
        return "删除成功，删除条目:"+delete;
    }

    /**
     * 查询全部，你也可以条件查询,排序查询，具体看MongoBaseService中封装的方法
     * @return
     */
    @RequestMapping(value = "/selectAll")
    public List<MongoTextInfo> selectAll(){
        List<MongoTextInfo> list = mongoTextService.listAll();
        return list;
    }

}
